//
//  NSString+IC_Html.m
//  InvestChina
//
//  Created by 闫建刚 on 14-5-29.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "NSString+IC_Html.h"

@implementation NSString (IC_Html)

// 处理IMG标签
-(NSString*)processImageTag {
    NSString *content = self;
    NSArray* matchesArr =  [content componentsMatchedByRegex:REG_EXPR_FOR_IMG_FOR_TAG];
    if ([matchesArr count] > 0) {
        for (NSString* imgStr in matchesArr) {
            NSString *imgTagReplaceStr = nil;
            NSString* srcStr = nil;
            NSArray *imgMatchArr = [imgStr componentsMatchedByRegex:REG_EXPR_FOR_IMG_FOR_SRC];
            srcStr = [imgMatchArr objectAtIndex:0];
            
            NSRange sindex;
            NSRange eindex;
            sindex = [srcStr rangeOfString:@"\'"];
            if (sindex.location == NSNotFound) {
                sindex = [srcStr rangeOfString:@"\""];
            }
            eindex = [srcStr rangeOfString:@"\'" options:NSBackwardsSearch];
            if (eindex.location == NSNotFound) {
                eindex = [srcStr rangeOfString:@"\"" options:NSBackwardsSearch];
            }
            
            srcStr = [srcStr substringWithRange:NSMakeRange(sindex.location + 1,eindex.location - sindex.location - 1)];
            imgTagReplaceStr = [[@"<img" stringByAppendingString:HTML_ATTR_ATTACH_IMG]stringByAppendingString:[imgStr substringFromIndex:4]];
            content = [content stringByReplacingOccurrencesOfString:imgStr withString:imgTagReplaceStr];
        }
    }
    return content;
}

@end
